import React ,{useState,useEffect}from 'react';
import {View,Text,TouchableOpacity,StyleSheet,ScrollView,Image} from 'react-native';
import '../../global'
const Schooldetail = ({navigation,route}) => {
  const [colors,setColors] = useState(['rgb(204,190,151)','rgb(234,234,234)','rgb(234,234,234)'])
  const [score,setScore]=useState([]);
  const [list,setList]=useState([]);
  const [recruit,setRecruit]=useState([{}]);
  const [tab,setTab]=useState('recruit')
  useEffect(()=>{
    fetch('https://wx.qasdwer.top:8080/school/recruit/',{
          method:'post',
          headers:{
            'content-type':'application/json;charset=utf-8'
          },
          body:JSON.stringify({name:route.params.school})
        })
    .then(res=>res.json())
    .then(res=>{ 
      setRecruit(res)
    })
    .catch((error)=>{
        console.log("Error");
        throw error;
      });

    fetch('https://wx.qasdwer.top:8080/school/score/',{
          method:'post',
          headers:{
            'content-type':'application/json;charset=utf-8'
          },
          body:JSON.stringify({name:route.params.school})
        })
    .then(res=>res.json())
    .then(res=>{ 
      setScore(res)
    })
    .catch((error)=>{
        console.log("Error");
        throw error;
      });

      fetch('https://wx.qasdwer.top:8080/school/number/',{
          method:'post',
          headers:{
            'content-type':'application/json;charset=utf-8'
          },
          body:JSON.stringify({name:route.params.school})
        })
    .then(res=>res.json())
    .then(res=>{ 
      setList(res)
    })
    .catch((error)=>{
        console.log("Error");
        throw error;
      }); 
  },[])
 
  const Card = ({name,navigation})=>{
    if(name=='recruit'){
        return (
          <TouchableOpacity onPress={()=>{navigation.navigate('RecruitDetail',{school:route.params.school,content:recruit[0].recruit_content})
          fetch('https://wx.qasdwer.top:8080/school/readnum',{
          method:'put',
          headers:{
            'content-type':'application/json;charset=utf-8'
          },
          body:JSON.stringify({name:route.params.school,num:recruit[0].readnum})
          })
          .then(res=>res.json())
          .then(res=>{ 
              setRecruit(res)
          })
          .catch((error)=>{
              console.log("Error");
              throw error;
          }); 
          }}>
          <View style={styles.list}>
            <Text style={styles.title}>{route.params.school}2022年硕士研究生招生简章</Text>
            <Text style={styles.content} numberOfLines={2}>{recruit[0].recruit_content}</Text>
            <Text style={styles.readnum}>{recruit[0].readnum}次阅读</Text>
          </View>
          </TouchableOpacity>
 
        )
    }
    else if(name=='score'){
        return (
          <View>  
          <View style={styles.scoreCon}>
            <Text style={{fontSize:changeW(30),fontWeight:'bold'}}>{route.params.school}计算机硕士考研近三年复试分数线</Text>
          </View>
          <View style={styles.score}>
            <View style={styles.scoreTitle}>
              <Text style={styles.scoreBox}>年份</Text>
              <Text style={styles.scoreBox}>政治</Text>
              <Text style={styles.scoreBox}>外语</Text>
              <Text style={styles.scoreBox}>专业一</Text>
              <Text style={styles.scoreBox}>专业二</Text>
              <Text style={styles.scoreBox}>总分</Text>
              <Text style={styles.scoreBox}>复试</Text>
              <Text style={styles.scoreBox}>录取</Text>
            </View>
            {
              score.map((item,index)=>{
                return (
                  <View style={styles.scoreTitle2} key={index}>
                    <Text style={styles.scoreBox}>{item.exam_year}</Text>
                    <Text style={styles.scoreBox}>{item.politics_score}</Text>
                    <Text style={styles.scoreBox}>{item.english_score}</Text>
                    <Text style={styles.scoreBox}>{item.major_score1}</Text>
                    <Text style={styles.scoreBox}>{item.major_score2}</Text>
                    <Text style={styles.scoreBox}>{item.whole_score}</Text>
                    <Text style={styles.scoreBox}>{item.reexam}</Text>
                    <Text style={styles.scoreBox}>{item.admit_score}</Text>
                  </View>
                )
              })
            }
          </View>
        </View>   
    )
    }
    else if(name=='number'){
        return (<View>
          <View style={styles.scoreCon}>
              <Text style={{fontSize:changeW(30),fontWeight:'bold'}}>{route.params.school}计算机硕士考研近三年报录比情况</Text>
            </View>
            <View style={styles.score}>
              <View style={styles.scoreTitle}>
                <Text style={styles.numberBox}>年份</Text>
                <Text style={styles.numberBox}>专业</Text>
                <Text style={styles.numberBox}>报名人数</Text>
                <Text style={styles.numberBox}>录取人数</Text>
                <Text style={styles.numberBox}>报录比</Text>
              </View>
              {
                list.map(item=>{
                  return (
                    <View style={styles.scoreTitle2}>
                      <Text style={styles.numberBox}>{item.exam_year}</Text>
                      <Text style={styles.numberBox}>计算机</Text>
                      <Text style={styles.numberBox}>{item.number1}</Text>
                      <Text style={styles.numberBox}>{item.number2}</Text>
                      <Text style={styles.numberBox}>{item.number3}</Text> 
                    </View>
                  )
                })
              }
            </View>
          </View>)
    

    }
}
    
   
  return (
    <ScrollView>
      <View style={styles.tabview}>
        <TouchableOpacity style={[styles.button,{backgroundColor:colors[0]}]}
          onPress={()=>{
              setColors(['rgb(204,190,151)','rgb(234,234,234)','rgb(234,234,234)','rgb(234,234,234)'])
              setTab('recruit')
          }}
        >
            <Text style={styles.text}>招生简章</Text>
        </TouchableOpacity>
          <TouchableOpacity style={[styles.button,{backgroundColor:colors[1]}]}
          onPress={()=>{
              setColors(['rgb(234,234,234)','rgb(204,190,151)','rgb(234,234,234)','rgb(234,234,234)'])
              setTab('score')
          }}
          >
            <Text style={styles.text}>考研分数线</Text>
        </TouchableOpacity>
          <TouchableOpacity style={[styles.button,{backgroundColor:colors[2]}]}
           onPress={()=>{
              setColors(['rgb(234,234,234)','rgb(234,234,234)','rgb(204,190,151)','rgb(234,234,234)'])
              setTab('number')
          }}
          >
            <Text style={styles.text}>考研报录比</Text>
          </TouchableOpacity>
      </View>
      

      <Card name={tab} navigation={navigation} ></Card>
    </ScrollView>
            
  )
}


  const styles=StyleSheet.create({
    list:{
        height:changeH(250),
        width:'95%',
        marginLeft:changeW(20),
        marginTop:changeH(20),
        position:'relative',
        backgroundColor:'white'
    },
    title:{
      position:'absolute',
      top:changeH(10),
      left:changeW(10),
      fontSize:changeW(30),
      fontWeight:'bold'
    },
    content:{
      position:'absolute',
      top:changeH(100),
      left:changeW(10),
      fontSize:changeW(25),
      color:"rgba(0,0,0,0.8)"
    },
    readnum:{
      position:'absolute',
      bottom:changeH(10),
      left:changeW(10),
      fontSize:changeW(20),
      color:"rgba(0,0,0,0.6)"
    },
    scoreCon:{
      marginTop:changeH(50),
      marginLeft:changeW(20)
    },
    score:{
      flexDirection:'column'
    },
    scoreTitle:{
      flexDirection:'row',
      justifyContent:'space-around',
      marginTop:changeH(50)
    },
    scoreTitle2:{
      width:'95%',
      marginLeft:changeW(20),
      flexDirection:'row',
      justifyContent:'space-between',
      marginTop:changeH(30)
    },
    scoreBox:{
      width:changeW(70),
      textAlign:'center'
    },
    numberBox:{
      width:changeW(100),
      textAlign:'center'
    },
    tabview:{
      flexDirection:'row',
      justifyContent:'space-around',
      marginTop:changeH(30),
      marginBottom:changeH(20)
    },
    button:{
      width:changeW(160),
      height:changeH(80),
      display:'flex',
      alignItems:'center',
      justifyContent:'center',
      borderRadius:40,
  },
  text:{
      fontSize:changeW(23)
  }
  })
  export default Schooldetail